﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using System.Web.UI;
using CustomsSystem.BLL;
using Microsoft.Win32;

namespace WEB.T_bill
{
    public partial class detailedAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["sheetID"] != null && Session["sheetID"]!= "")
            {
                gvBind();
                gridViewClass.GroupRows(gvfeeInfo ,2);
              
            }


        }

        public void gvBind()
        {
            int a = int.Parse(Session["sheetID"].ToString());

            CustomsSystem.BLL.SheetInfoManage bll = new CustomsSystem.BLL.SheetInfoManage();
            string strSql = " sheetID=" + a;
            DataSet ds = new DataSet();
            ds = bll.GetList(strSql);
            if (ds != null)
            {
                gvsheetInfo.DataSource = ds;//绑定委托单信息
                gvsheetInfo.DataBind();
            }
            else
            {
                MessageBox.ShowAndRedirect(this, "该单还没有费用数据", "billView.aspx");
            }
            //获取客户资料
            CustomsSystem.Model.Customer customer = new CustomsSystem.BLL.CustomerManage().GetModel(int.Parse(ds.Tables[0].Rows[0][3].ToString()));//获取客户公司信息
            lbCID.Text = customer.CustomerName;
            lbTel.Text = customer.CustomerPhone;
            lbFax.Text = customer.Fax;
            lbtime.Text = DateTime.Now.ToShortDateString();

            //获取该单费用
            CustomsSystem.BLL.BillManage billmanage = new CustomsSystem.BLL.BillManage();

            DataSet dsbill = new DataSet();

            dsbill = billmanage.GetFeeList(" sheetID=" + a + " and billTypeID=1 ");
            //给该表添加合计行
            if (dsbill != null)
            {
                DataTable dt = dsbill.Tables[0];
                DataRow nr1 = dt.NewRow();
                DataRow nr2 = dt.NewRow();
                nr2[2] = "合计";
                decimal sum = 0; string b;
                for (int i = 0; i < dsbill.Tables[0].Rows.Count; i++)
                {
                    b = dt.Rows[i][4].ToString();
                    if (b != null && b != "")
                        sum += Convert.ToDecimal(b);
                }
                nr2[4] = sum;
                dt.Rows.Add(nr1);
                dt.Rows.Add(nr2);
                dt.AcceptChanges();
                gvfeeInfo.DataSource = dt;
                gvfeeInfo.DataBind();
                //获取审核人的名字
               string ak= dsbill.Tables[0].Rows[0][1].ToString();
                if (ak != "0" && ak != null)
                {
                    CustomsSystem.Model.Users user = new CustomsSystem.BLL.UserManage().GetModel(ak);
                    if(user !=null )
                    lbaudit.Text = user.UserName;
                   // else MessageBox.ShowAndRedirect(this, "该单未被审核，请在审核后再操作", "billView.aspx");
                }
            }

            else MessageBox.ShowAndRedirect(this, "该单未被审核，请在审核后再操作", "billView.aspx");
        }

        protected void gvfeeInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

           
            e.Row.Cells[0].Visible = false;//让第1列不可见
            e.Row.Cells[1].Visible = false;//2
        }

        protected void btnDerive_Click(object sender, EventArgs e)
        {
            
            int a = int.Parse(Session["sheetID"].ToString());
            string[] info = new string[] { lbCID.Text, lbTel.Text,lbFax.Text  , "中国珠海外轮代理有限公司" + lbemp.Text, "0756-7716607", "0756-7269213",lbtime.Text ,lbaudit .Text  };
           

            billDataToExcel datatoExcel = new billDataToExcel();
            BillManage dll = new BillManage();
            string filepath = Server.MapPath("../document/Excel/");
            DataTable dt= dll.GetFeeList(" sheetID = "+a+" and billTypeID=1 ").Tables[0];
            CustomsSystem.BLL.SheetInfoManage bll = new CustomsSystem.BLL.SheetInfoManage();
            DataSet ds= bll.GetMainInfo ( "sheetID= "+a);
            DataTable dt1 = ds.Tables[0];
            dt.Columns.Remove(dt.Columns["billID"]);
            dt.Columns.Remove(dt.Columns["auditEmpID"]);
            string file = datatoExcel.OutputExcelo(dt, dt1, "报关费用小结", filepath, info);
            DownloadFile(file);
        }

        //// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        protected void DownloadFile(string file)
        {
            string filename = "/document/Excel/" + file;
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            //对文件进行编码，防止下载名出现乱码
            string encodefileName = System.Web.HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8);

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();

        }


        protected void btnT_Click(object sender, EventArgs e)
        {
            if (ddl1.SelectedIndex == 1)
            { Response.Redirect("detailedAccount2.aspx"); }
        }
       
    }
}